USE [ServerMonitor]
GO
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[GetFileTime]') AND type in (N'FN', N'IF', N'TF', N'FS', N'FT'))
DROP FUNCTION [dbo].[GetFileTime]
GO
USE [ServerMonitor]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE FUNCTION [dbo].[GetFileTime](@name NVARCHAR(256))
RETURNS DATETIME
AS
BEGIN
  DECLARE @FileTime DATETIME, @DateTimeStr VARCHAR(256)
  SET @name = LTRIM(RTRIM(@name))
  IF(RIGHT(@name,4) IN ('.TRN','.BAK')) SET @name = SUBSTRING(@name,1,LEN(@name) -4)
  IF(CHARINDEX('_LOG_',@name) != 0) SET @name = RIGHT(@name,LEN(@name) - CHARINDEX('_LOG_',@name) -4)
  IF(CHARINDEX('_DATA_',@name) != 0) SET @name = RIGHT(@name,LEN(@name) - CHARINDEX('_DATA_',@name) -5)
  IF(ISNUMERIC(@name) = 1 AND LEN(@name) = 14)
  SET @DateTimeStr = SUBSTRING(@name,1,8) + ' '
                   + SUBSTRING(@name,9,2) + ':'
                   + SUBSTRING(@name,11,2) + ':'
                   + SUBSTRING(@name,13,2)
  SET @FileTime = CONVERT(DATETIME,@DateTimeStr)
  RETURN(@FileTime)
END




GO


